/**
 * Copyright &copy; 2012-2014 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved.
 */
package com.thinkgem.jeesite.modules.inventory.service;

import com.thinkgem.jeesite.common.config.Global;
import com.thinkgem.jeesite.common.persistence.Page;
import com.thinkgem.jeesite.common.service.CrudService;
import com.thinkgem.jeesite.common.utils.Collections3;
import com.thinkgem.jeesite.common.utils.StringUtils;
import com.thinkgem.jeesite.modules.inventory.dao.HostInventoryDao;
import com.thinkgem.jeesite.modules.inventory.dao.ZlmtCabinetDao;
import com.thinkgem.jeesite.modules.inventory.dao.ZlmtHostsCabinetDao;
import com.thinkgem.jeesite.modules.inventory.entity.HostInventory;
import com.thinkgem.jeesite.modules.inventory.entity.ZlmtCabinet;
import com.thinkgem.jeesite.modules.inventory.entity.ZlmtHostsCabinet;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

/**
 * 资产管理Service
 * @author johnKee
 * @version 2016-07-20
 */
@Service
@Transactional(readOnly = true)
public class HostInventoryService extends CrudService<HostInventoryDao, HostInventory> {

	@Autowired
	private ZlmtHostsCabinetDao zlmtHostsCabinetDao;
	@Autowired
	private ZlmtCabinetDao zlmtCabinetDao;

	public HostInventory get(String id) {
		return super.get(id);
	}
	
	public List<HostInventory> findList(HostInventory hostInventory) {
		return super.findList(hostInventory);
	}

	public List<HostInventory> findListWithLoc(HostInventory hostInventory) {
		return dao.findDatailedList(hostInventory);
	}

	public Page<HostInventory> findPage(Page<HostInventory> page, HostInventory hostInventory) {
		return super.findPage(page, hostInventory);
	}

	public List<Map> findDetailedPage(HostInventory hostInventory) {
		List<Map> resultList = new ArrayList<Map>();

		List<ZlmtCabinet> cabinetList = zlmtCabinetDao.findList(new ZlmtCabinet());
		if(!Collections3.isEmpty(cabinetList)){
			Map<String, Object> map = new HashMap<String, Object>();
			map.put("row", cabinetList.get(0).getRow().longValue()+"");
			map.put("hostCount", 0);
			map.put("cabinetCount", 0);
			resultList.add(map);
			for(ZlmtCabinet cabinet:cabinetList){
				if(cabinet.getRow().longValue() == Long.parseLong(map.get("row").toString())){
					map.put("cabinetCount", Integer.parseInt(map.get("cabinetCount").toString()) + 1);
				}else {
					map = new HashMap<String, Object>();
					map.put("row", cabinet.getRow().longValue()+"");
					map.put("hostCount", 0);
					map.put("cabinetCount", 1);
					resultList.add(map);
				}
			}
		}
		List<HostInventory> hostList = dao.findDatailedList(hostInventory);
		if(!Collections3.isEmpty(hostList)){
			for(HostInventory hostEntry:hostList){
				Map map = null;
				for(Map mapEntry:resultList){
					if(Long.parseLong(mapEntry.get("row").toString()) == hostEntry.getRow().longValue()){
						map = mapEntry;
						break;
					}
				}
				map.put("hostCount", Integer.parseInt(map.get("hostCount").toString()) + 1);
				String hostKey = Long.parseLong(hostEntry.getRow().toString())+"-"+Long.parseLong(hostEntry.getColumns().toString())+"-"+hostEntry.getUnitUsedCode();
				Map<String, String> hostMap = new HashMap<String, String>();
				hostMap.put("isMonitoring",hostEntry.getInventoryMode().equals("1")?"Y":"N");
				hostMap.put("hostIp", hostEntry.getTag());
				hostMap.put("sys", hostEntry.getSoftware());
				hostMap.put("serialNumber",Long.parseLong(hostEntry.getRow().toString())+"-"+Long.parseLong(hostEntry.getColumns().toString())+"-"+hostEntry.getCabinetCode()+"-"+hostEntry.getUnitUsedCode()+"-"+hostEntry.getHostLocCode());
				String hostName = hostEntry.getName();
				//过滤名称中包括空格信息
				if(hostName != null && hostName.length() > 0){
					hostName = hostName.replaceAll(" ", "");
				}
				hostMap.put("hostName", hostName);
				hostMap.put("type", hostEntry.getUnitUsed()+"u");
				hostMap.put("status", "off");
				String category = hostEntry.getType();
				if(category != null && category.length() > 0){
					category = category.replaceAll(" ", "");
				}
				hostMap.put("category", category);
				map.put(hostKey, hostMap);
			}
		}
		return resultList;
	}
	
	@Transactional(readOnly = false)
	public void save(HostInventory hostInventory) {
		if(StringUtils.isNotEmpty(hostInventory.getCabinetId())){
			ZlmtHostsCabinet zlmtHostsCabinet = new ZlmtHostsCabinet();
			zlmtHostsCabinet.setHostid(hostInventory.getId());
			List<ZlmtHostsCabinet> oldValues = zlmtHostsCabinetDao.findList(zlmtHostsCabinet);
			if(!Collections3.isEmpty(oldValues)){
				for(ZlmtHostsCabinet hostsCabinet:oldValues){
					hostsCabinet.setThruTime(new Date());
					zlmtHostsCabinetDao.update(hostsCabinet);
				}
			}
			zlmtHostsCabinet.setCabinetId(hostInventory.getCabinetId());
			zlmtHostsCabinet.setFromTime(new Date());
			zlmtHostsCabinet.setThruTime(Global.THRU_TIME);
			zlmtHostsCabinet.setHostLocCode(hostInventory.getHostLocCode());
			zlmtHostsCabinet.setUnitUsed(hostInventory.getUnitUsed());
			zlmtHostsCabinet.setUnitUsedCode(hostInventory.getUnitUsedCode());
			zlmtHostsCabinet.preInsert();
			zlmtHostsCabinetDao.insert(zlmtHostsCabinet);
		}
		super.save(hostInventory);
	}
	
	@Transactional(readOnly = false)
	public void delete(HostInventory hostInventory) {
		super.delete(hostInventory);
	}
	
}